﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.IO;
using System.Security;
using System.Net;

namespace Business.ApplicationSecurity
{
    public class Declarative_Imperative_Security_to_Protect_Methods
    {
        [FileIOPermission(SecurityAction.Demand, Write = @"C:\Temp", Unrestricted = true)]
        public static void CreateProgramFolder_Declarative()
        {
            CreateFolderBusiness();
        }
       

        [WebPermission(SecurityAction.Demand, ConnectPattern = "http://www.microsoft.com/.*")]
        public static void RequestWebPage_Declarative()
        {
            try
            {
                Console.WriteLine("Starting request ...");
                WebRequest request = WebRequest.Create("http://www.microsoft.com");
                Console.WriteLine("Starting response ...");
                WebResponse response = request.GetResponse();

                Console.WriteLine(response.ContentType + " " + response.ResponseUri.AbsoluteUri);
                Console.WriteLine("Done ...");
            }
            catch (WebException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }


        private static void CreateFolderBusiness()
        {
            CreateRequestResponseTest();
        }
        private static void CreateRequestResponseTest()
        {
            try
            {
                Console.WriteLine("Starting request ...");
                WebRequest request = WebRequest.Create("http://www.microsoft.com");
                Console.WriteLine("Starting response ...");
                WebResponse response = request.GetResponse();

                Console.WriteLine(response.ContentType + " " + response.ResponseUri.AbsoluteUri);
                Console.WriteLine("Done ...");
            }
            catch (WebException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }


        public static void CreateProgramFolder_Imperative()
        {
            Console.WriteLine("START CreateProgramFolder_Imperative");

            FileIOPermission filePermisions = new FileIOPermission(FileIOPermissionAccess.Write, @"C:\Temp\");
            filePermisions.Demand();
            CreateFolderBusiness();

            Console.WriteLine("STOP CreateProgramFolder_Imperative");
        }

        public static void RequestWebPage_Imperative()
        {
            Console.WriteLine("START RequestWebPage_Imperative");

            WebPermission webPermisson = new WebPermission(NetworkAccess.Connect, new System.Text.RegularExpressions.Regex(@"http://www.microsoft.com/.*"));
            webPermisson.Deny();
            CreateRequestResponseTest();

            Console.WriteLine("STOP RequestWebPage_Imperative");
        }

        
    
    }
}
